﻿using Albatross.SharePoint.Configuration;
using Albatross.WPF.Common;
using Albatross.WPF.Common.ViewModels;
using Albatross.WPF.Common.Views;
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Albatross.SharePoint.Explorer.ViewModels {
	public class ExplorerViewModel : WorkspaceViewModel{
		public ExplorerViewModel(SPSiteCfgElement cfgElement){
			CfgElement = cfgElement;
			Context = new ClientContext(CfgElement.Url);
			Sites = new ObservableCollection<ClientObjectViewModel>();
			Title = cfgElement.Name;
		}

		#region properties
		public SPSiteCfgElement CfgElement {
			get;
			private set;
		}
		public ClientContext Context { get; private set; }

		public ObservableCollection<ClientObjectViewModel> Sites { get; private set; }

		public const string SelectedTreeItemPropertyName = "SelectedTreeItem";
		private ClientObjectViewModel _selectedTreeItem;
		public ClientObjectViewModel SelectedTreeItem {
			get { return _selectedTreeItem; }
			set {
				if (_selectedTreeItem == value) { return; }
				RaisePropertyChanging(SelectedTreeItemPropertyName);
				_selectedTreeItem = value;
				RaisePropertyChanged(SelectedTreeItemPropertyName);
			}
		}

		public const string ViewServicePropertyName = "ViewService";
		private IViewService _viewSvcs;
		public IViewService ViewService {
			get { return _viewSvcs; }
			set {
				if (_viewSvcs == value) { return; }
				RaisePropertyChanging(ViewServicePropertyName);
				_viewSvcs = value;
				RaisePropertyChanged(ViewServicePropertyName);
			}
		}
		#endregion

		#region commands
		public RelayCommand<IViewService> RefreshCommand { get { return new RelayCommand<IViewService>(Refresh); } }
		public RelayCommand<CommandParameter> SelectedItemChangedCommand { get { return new RelayCommand<CommandParameter>(SelectedItemChanged); } }
		void SelectedItemChanged(CommandParameter cmdParam) {
			ClientObjectViewModel vm = cmdParam.Value as ClientObjectViewModel;
			if (vm != null) {
				vm.LoadAsync(cmdParam.ViewService);
				if (cmdParam.ViewService != null) {
					cmdParam.ViewService.ShowWorkspace(vm);
				}
			}
		}
		#endregion

		#region methods
		void Refresh(IViewService svcs) {
			if (SelectedTreeItem != null) {
				SelectedTreeItem.Refresh(svcs);
			} else {
				LoadAsync(svcs);
			}
		}
		public override void LoadAsync(IViewService svcs) {
			try {
				IsLoading = true;
				Sites.Clear();
				ClientObjectViewModel site = ClientObjectFactory.GetViewModel(Context, "Site", typeof(Site), Context.Site);
				site.ReloadObject = true;
				Sites.Add(site);
				site.LoadAsync(svcs);
				site.IsExpanded = true;
			} catch (Exception err) {
				Log.Error(err);
				svcs.ShowErrorMessageBox("An error has occurred while loading the site");
			} finally {
				IsLoading = false;
			}
		}
		protected override void Dispose(bool disposing) {
			if (disposing) {
				base.Dispose(disposing);
				if (Context != null) {
					Context.Dispose();
				}
			}
		}
		#endregion
	}
}
